﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeBuilder.DevTools
{
    public partial class HtmlTextWriterUtilityForm : UserControl
    {
        public HtmlTextWriterUtilityForm()
        {
            InitializeComponent();
        }

        #region Helper Methods

        private bool UseStringBuilder()
        {
            return useStringBuilderCheckBox.Checked;
        }

        private void CheckButtons()
        {
            string instanceName = instanceNameTextBox.Text.Trim();
            string content = contentTextBox.Text.Trim();

            if (String.IsNullOrEmpty(instanceName) || String.IsNullOrEmpty(content))
            {
                generateButton.Enabled = false;
            }
            else
            {
                generateButton.Enabled = true;
            }
        }

        private void TrimEmptyLines(TextBox textBox)
        {
            List<string> result = new List<string>();
            bool foundNotEmptyLine = false;

            // Remove all empty line at the beginning
            foreach (string line in textBox.Lines)
            {
                if (!String.IsNullOrEmpty(line.Trim()) || foundNotEmptyLine)
                {
                    result.Add(line);
                    foundNotEmptyLine = true;
                }
            }

            // Remove all empty lines at the end
            for (int i = result.Count - 1; i >= 0; i--)
            {
                if (String.IsNullOrEmpty(result[i]))
                {
                    result.RemoveAt(i);
                }
                else
                {
                    break;
                }
            }

            textBox.Lines = result.ToArray();
        }

        private void SwitchBackColor(Control control)
        {
            if (control.BackColor == SystemColors.Window)
            {
                control.BackColor = SystemColors.Info;
            }
            else
            {
                control.BackColor = SystemColors.Window;
            }
        }

        #endregion


        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void generateButton_Click(object sender, EventArgs e)
        {
            string instanceName = instanceNameTextBox.Text.Trim();
            StringBuilder content = new StringBuilder();

            TrimEmptyLines(contentTextBox);

            if (UseStringBuilder())
            {
                content.Append("StringBuilder builder = new StringBuilder();");
                content.AppendLine();
                content.AppendLine();
            }

            foreach (string line in contentTextBox.Lines)
            {
                if (UseStringBuilder())
                {
                    content.Append("builder.Append(\"");
                }
                else
                {
                    content.Append(instanceName + ".Write(\"");
                }
                content.Append(line.Replace("\"", "\\\""));
                content.AppendLine("\");");
            }

            if (UseStringBuilder())
            {
                content.AppendLine();
                content.Append(instanceName + ".Write(builder.ToString());");
            }

            contentTextBox.Text = content.ToString();
        }

        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void copyButton_Click(object sender, EventArgs e)
        {
            string content = contentTextBox.Text.Trim();
            Clipboard.SetText(content, TextDataFormat.Text);
            contentTextBox.SelectAll();
        }

        /// <summary>
        /// Reset
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void resetButton_Click(object sender, EventArgs e)
        {
            instanceNameTextBox.Text = "writer";
            contentTextBox.Text = string.Empty;
            lineWrapCheckBox.Checked = false;
            useStringBuilderCheckBox.Checked = false;
            contentTextBox.Select();
        }

        private void HtmlTextWriterUtilityForm_Load(object sender, EventArgs e)
        {
            contentTextBox.Select();
        }

        private void contentTextBox_TextChanged(object sender, EventArgs e)
        {
            CheckButtons();
            string content = contentTextBox.Text.Trim();
            if (String.IsNullOrEmpty(content))
            {
                copyButton.Enabled = false;
            }
            else
            {
                copyButton.Enabled = true;
            }
        }

        private void lineWrapCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            contentTextBox.WordWrap = lineWrapCheckBox.Checked;
        }

       
    }
}
